package conexao;


import java.io.IOException;

import javax.media.DataSink;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoDataSinkException;
import javax.media.NoDataSourceException;
import javax.media.NoPlayerException;
import javax.media.NoProcessorException;
import javax.media.protocol.DataSource;

import audio.Captura;
import audio.MicrofoneAusenteException;

public class ServidorRTP extends Thread{
	
	private MediaLocator destino;
	private Captura voz;
	private DataSink emissor;	
		
    public ServidorRTP(String ipDestino, int porta) throws NoDataSourceException, NoPlayerException, MicrofoneAusenteException, IOException {    	
    	this.destino = new MediaLocator("rtp://"+ipDestino+":"+porta+"/audio/1");
    	this.voz = new Captura();
    }    
    
    public MediaLocator getDestino() {
		return destino;
	}

	public void setDestino(MediaLocator destino) {
		this.destino = destino;
	}

	public Captura getVoz() {
		return voz;
	}

	public void setVoz(Captura voz) {
		this.voz = voz;
	}

	public DataSink getEmissor() {
		return emissor;
	}

	public void setEmissor(DataSink emissor) {
		this.emissor = emissor;
	}

	public void enviar() throws NoDataSinkException, SecurityException, IOException, NoProcessorException{
    	DataSource ds = this.voz.configuraRTP();
    	emissor = Manager.createDataSink(ds,this.destino);
    	emissor.open();
    	emissor.start();
    	this.voz.capturar();
    }   
    
    @Override
	public void run(){ 
		try {
			this.enviar();
		} catch (NoProcessorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoDataSinkException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args){
    	
		ServidorRTP teste;
		try {
			teste = new ServidorRTP("172.16.1.209", 5000);
			teste.start();
		} catch (NoDataSourceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoPlayerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MicrofoneAusenteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
    }
}


